In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
import plotly.express as px
import pymc3
import plotly.graph_objects as go
import plotly.figure_factory as ff
In [2]:
size=20000
#facebook
facebook_budget=15
ads_days=5
conversion=[17,48]
reach=[527,1500]

#website conversions
sucess=3
trials=100
simplifier=1

#ecommerce
ali_price=6.19
shipping=0
selling_price=(ali_price+shipping)+(ali_price+shipping)*2.5
#selling_price=20

#payment_gate
two_checkout=(selling_price+shipping)*0.035+0.35

#arrays
prior_arr=np.empty(size)
likelihood_arr=np.empty(size)

    
for i in range(size):
    
    prior_ads=np.random.beta(np.random.randint(conversion[0],conversion[1]),(np.random.randint(reach[0],reach[1])-np.random.randint(conversion[0],conversion[1])),size=ads_days)
    likelihood_ads=np.sum(np.random.binomial(np.random.randint(reach[0],reach[1]),prior_ads,size=ads_days))
    

    
    prior=np.random.beta(sucess/simplifier,(trials-sucess)/simplifier,size=ads_days)
    likelihood=np.sum(np.random.binomial(likelihood_ads,prior,size=ads_days))
    
    prior_arr[i]=round(np.mean(prior)*100,2)
    likelihood_arr[i]=likelihood
    
df=pd.DataFrame(data={"Conversion rate":prior_arr,"Conversions":likelihood_arr})

df['Revenue']=df["Conversions"]*selling_price
df['Cost of Sales']=df["Conversions"]*(ali_price+shipping)
df['Gross profit']=df['Revenue']-df['Cost of Sales']
df["Expences"]=ads_days*facebook_budget+20+12
df['Operation Profit']=df['Gross profit']-df["Expences"]
df['Tax']=df['Operation Profit']*0.2
df['Net Profit']=df['Operation Profit']-df['Tax']

df['Investment Nedded']=df['Cost of Sales']+df["Expences"]
df["Profit per unit"]=round(df["Net Profit"]/df['Conversions'],2)
df['Return on investment']=round(df['Net Profit']/df['Investment Nedded']*100)

print("Selling price is:",selling_price)
for i in ["Net Profit","Investment Nedded",'Return on investment',"Conversion rate","Conversions",'Profit per unit']:
    fig1=px.histogram(df,x=i,marginal='box')

    fig1.add_trace(
        go.Scatter(
            x=[pymc3.stats.hpd(df[i])[0], pymc3.stats.hpd(df[i])[0]],
            y=[0, 200],
            mode="lines",
            line=go.scatter.Line(color="red"),
            showlegend=False))
    fig1.add_trace(
        go.Scatter(
            x=[pymc3.stats.hpd(df[i])[1], pymc3.stats.hpd(df[i])[1]],
            y=[0, 200],
            mode="lines",
            line=go.scatter.Line(color="red"),
            showlegend=False))
    if i == "Conversion rate":
        print("The HPD of the "+i+" is bettwen",round(pymc3.stats.hpd(df[i])[0],2),"and",round(pymc3.stats.hpd(df[i])[1],2))
    else:
        print("The HPD of the "+i+" is bettwen",round(pymc3.stats.hpd(df[i])[0]),"and",round(pymc3.stats.hpd(df[i])[1]))
    fig1.show()
    print("________________________________________________")
Selling price is: 21.665000000000003
The HPD of the Net Profit is bettwen -61 and 608
________________________________________________
The HPD of the Investment Nedded is bettwen 119 and 454
________________________________________________
The HPD of the Return on investment is bettwen 1 and 145
________________________________________________
The HPD of the Conversion rate is bettwen 1.61 and 4.37
________________________________________________
The HPD of the Conversions is bettwen 2 and 56
________________________________________________
The HPD of the Profit per unit is bettwen 2 and 12
________________________________________________
In [ ]:
 
In [3]:
df.head()
Out[3]:
Conversion rate Conversions Revenue Cost of Sales Gross profit Expences Operation Profit Tax Net Profit Investment Nedded Profit per unit Return on investment
0 2.82 23.0 498.295 142.37 355.925 107 248.925 49.785 199.14 249.37 8.66 80.0
1 4.21 23.0 498.295 142.37 355.925 107 248.925 49.785 199.14 249.37 8.66 80.0
2 3.87 11.0 238.315 68.09 170.225 107 63.225 12.645 50.58 175.09 4.60 29.0
3 2.87 17.0 368.305 105.23 263.075 107 156.075 31.215 124.86 212.23 7.34 59.0
4 2.51 11.0 238.315 68.09 170.225 107 63.225 12.645 50.58 175.09 4.60 29.0
In [ ]:
 
In [4]:
df2=df
df2=df2[df2['Conversion rate']<=2]
#df2=df2[df2['Investment Nedded']<=500]

print("Selling price is:",selling_price)
for i in ["Net Profit","Investment Nedded",'Return on investment',"Conversion rate","Conversions",'Profit per unit']:
    fig1=px.histogram(df2,x=i,marginal='box')

    fig1.add_trace(
        go.Scatter(
            x=[pymc3.stats.hpd(df2[i])[0], pymc3.stats.hpd(df2[i])[0]],
            y=[0, 200],
            mode="lines",
            line=go.scatter.Line(color="red"),
            showlegend=False))
    fig1.add_trace(
        go.Scatter(
            x=[pymc3.stats.hpd(df2[i])[1], pymc3.stats.hpd(df2[i])[1]],
            y=[0, 200],
            mode="lines",
            line=go.scatter.Line(color="red"),
            showlegend=False))
    if i == "Conversion rate":
        print("The HPD of the "+i+" is bettwen",round(pymc3.stats.hpd(df2[i])[0],2),"and",round(pymc3.stats.hpd(df2[i])[1],2))
    else:
        print("The HPD of the "+i+" is bettwen",round(pymc3.stats.hpd(df2[i])[0]),"and",round(pymc3.stats.hpd(df2[i])[1]))
    fig1.show()
    print("________________________________________________")
Selling price is: 21.665000000000003
The HPD of the Net Profit is bettwen -61 and 311
________________________________________________
The HPD of the Investment Nedded is bettwen 119 and 305
________________________________________________
The HPD of the Return on investment is bettwen -27 and 117
________________________________________________
The HPD of the Conversion rate is bettwen 1.4 and 2.0
________________________________________________
The HPD of the Conversions is bettwen 2 and 32
________________________________________________
The HPD of the Profit per unit is bettwen -9 and 10
________________________________________________
In [ ]:
 
In [ ]:
 
In [ ]: